//
//  ZipArchiveManager.m
//  DownLoadTest
//
//  Created by xiong qi on 13-4-12.
//  Copyright (c) 2013年 xiong qi. All rights reserved.
//

#import "ZipArchiveManager.h"
#import "ZipArchive.h"
#import "CarInfo.h"
#import "ConfigCodeHelper.h"

@implementation ZipArchiveManager

static ZipArchiveManager * instance;

+(ZipArchiveManager *)SharedInstance
{
    if (instance == nil) {
        instance = [ZipArchiveManager new];
    }
    return instance;
}

-(id)init
{
    self = [super init];
    if (self) {
        
        moniterThread = [[NSThread alloc] initWithTarget:self selector:@selector(onMoniterThread) object:nil];
		[moniterThread start];
    }
    return self;
}

//后台线程，主要用于解压文件
-(void)onMoniterThread
{
	NSAutoreleasePool* pool = [NSAutoreleasePool new];
	
	NSRunLoop* loop = [NSRunLoop currentRunLoop];
	
	NSTimer* timer = [[NSTimer alloc] initWithFireDate:nil
											  interval:1
												target:self
											  selector:@selector(onMoniterThreadUpdate)
											  userInfo:nil
											   repeats:YES];
	
	[loop addTimer:timer forMode:NSDefaultRunLoopMode];
	
	while([loop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
	
	[timer invalidate];
	SafeRelease(timer);
	SafeRelease(pool);
}

-(void)onMoniterThreadUpdate
{
	//do nothing
}

- (void)decompressionWithFilePath:(NSString*)filePath {
    [self performSelector:@selector(onDecompressionFilePath:) onThread:moniterThread withObject:filePath waitUntilDone:NO];
}

- (void)onDecompressionFilePath:(NSString*)filePath {
    ZipArchive* zip = [[ZipArchive alloc] init];
    BOOL ret = NO;
    if( [zip UnzipOpenFile:filePath] ) {
        
        NSError* error = nil;
        [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/company", [ConfigCodeHelper shareInstance].padDataPath] error:&error];
        if (nil != error) {
            NSLog(@"error:%@", error);
        }
        
        ret = [zip UnzipFileTo:[NSString stringWithFormat:@"%@/company", [ConfigCodeHelper shareInstance].padDataPath] overWrite:YES];
        
        [zip UnzipCloseFile];
    }
    [zip release];
}

-(void)decompressionWithCarInfo:(CarInfo *)carInfo
{
    [self performSelector:@selector(onDecompressCarFile:) onThread:moniterThread withObject:carInfo waitUntilDone:NO];
}

-(void)onDecompressCarFile:(CarInfo *)carInfo
{
    ZipArchive* zip = [[ZipArchive alloc] init];
    BOOL ret = NO;
    if( [zip UnzipOpenFile:carInfo.downloadCarObj.saveFilePath] ) {
        ret = [zip UnzipFileTo:carInfo.downloadCarObj.saveAutosPath overWrite:YES];
        if (ret) {
            carInfo.downloadCarObj.state = DecompressionFinish_State;
            [self performSelectorOnMainThread:@selector(sendDecompressionFinishNotification:) withObject:carInfo waitUntilDone:NO];
        }
        else
        {
            carInfo.downloadCarObj.state = DecompressionError_State;
            [self performSelectorOnMainThread:@selector(sendDecompressionErrorNotification:) withObject:carInfo waitUntilDone:NO];
        }
            
        [zip UnzipCloseFile];
    } else {
        carInfo.downloadCarObj.state = DecompressionError_State;
        [self performSelectorOnMainThread:@selector(sendDecompressionErrorNotification:) withObject:carInfo waitUntilDone:NO];
    }
    
    [zip release];
    
    
}

-(void)sendDecompressionFinishNotification:(CarInfo*)object {
    [[NSNotificationCenter defaultCenter] postNotificationName:Decompress_Complete object:object];
}

-(void)sendDecompressionErrorNotification:(CarInfo*)object {
    [[NSNotificationCenter defaultCenter] postNotificationName:Download_State_Change object:object];
}

-(void)dealloc
{
    [moniterThread release];
    [super dealloc];
}


@end
